<template>
  <van-icon @click="onChange" :color="comment===1 ? 'red' :'#777'" :name="comment===1 ? 'good-job' :'good-job-o'" />
</template>

<script>
import { addLikes, deleteLikes } from '@/api/article.js'
export default {
  props: {
    comment: {
      type: Number,
      required: true
    },
    artId: {
      type: [Object, Number],
      required: true
    }
  },
  created () {
    console.log('comment')
  },
  model: {
    prop: 'comment'
  },
  methods: {
    async onChange () {
      try {
        if (this.comment === 1) {
          await deleteLikes(this.artId)
          this.$emit('input', -1)
          // 已点赞，点击取消
        } else {
          await addLikes(this.artId)
          this.$emit('input', 1)
          // 未点赞，点击添加
        }
        this.$toast('操作成功')
      } catch (err) {
        this.$toast('操作失败')
      }
    }
  }
}
</script>

<style>
</style>
